﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using Jy123.Web.AppCode;
using Jy123.Common;

namespace Jy123.Web.Manage
{
    public partial class Line : MgConfig
    {
        private Dictionary<int, string> sportTypeDic = new Dictionary<int, string>();
        private List<Jy123.Model.Public_SportType> listSportType;
        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("Line.aspx");
            Jy123.BLL.Public_SportType BLLSportType = new Jy123.BLL.Public_SportType();
            listSportType = BLLSportType.GetModelList(string.Empty);
            foreach (Jy123.Model.Public_SportType sportType in listSportType)
            {
                sportTypeDic.Add(sportType.ID, sportType.SName);
            }
            if (!Page.IsPostBack)
            {
                LoadInitialValue();
            }
        }

        protected void Repeater_DataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Jy123.Model.Public_Line line = e.Item.DataItem as Jy123.Model.Public_Line;
                HiddenField idHidden = e.Item.FindControl("idHidden") as HiddenField;
                idHidden.Value = line.ID.ToString();
                Label nameLabel = e.Item.FindControl("lineNameLabel") as Label;
                nameLabel.Text = line.LName;
                Label sportTypeLabel = e.Item.FindControl("sportTypeLabel") as Label;
                sportTypeLabel.Text = sportTypeDic[line.FSportType.Value];
                Image lineImage = e.Item.FindControl("lineImage") as Image;
                lineImage.ImageUrl = line.LModel;
            }
        }

        protected void Repeater_Command(object sender, RepeaterCommandEventArgs e)
        {
            RepeaterItem item = e.Item as RepeaterItem;
            HiddenField hidden = e.Item.FindControl("idHidden") as HiddenField;
            int id = int.Parse(hidden.Value);
            Jy123.BLL.Public_Line BLLLine = new Jy123.BLL.Public_Line();
            if (e.CommandName.Equals("Edit"))
            {
                Jy123.Model.Public_Line line = BLLLine.GetModel(id);
                actionTypeHidden.Value = "Update";
                idHidden.Value = id.ToString();
                lineNameTextBox.Text = line.LName;                
                if (line.FSportType.HasValue)
                {
                    sportTypeDropDown.SelectedValue = line.FSportType.Value.ToString();
                }                
                lineImage.Src = line.LModel;
            }
            else if (e.CommandName.Equals("Delete"))
            {
                BLLLine.Delete(id);                
                LoadInitialValue();
                ClearPage();
            }
        }

        protected void Save(object sender, EventArgs e)
        {
            Jy123.BLL.Public_Line BLLLine = new Jy123.BLL.Public_Line();
            if (actionTypeHidden.Value.Equals("Add"))
            {
                Jy123.Model.Public_Line line = new Jy123.Model.Public_Line();
                SetValue(line);
                int typeId = BLLLine.Add(line);
                line.LModel = CreateLogo(typeId);
                line.ID = typeId;
                BLLLine.Update(line);
            }
            else
            {
                int id = int.Parse(idHidden.Value);
                Jy123.Model.Public_Line line = BLLLine.GetModel(id);
                SetValue(line);
                if (fileUpload.PostedFile != null)
                {
                    string strLogo = CreateLogo(id);
                    if (!string.IsNullOrEmpty(strLogo))
                    {
                        line.LModel = strLogo;
                    }
                }

                BLLLine.Update(line);
            }            
            LoadInitialValue();
            ClearPage();
        }

        private void SetValue(Jy123.Model.Public_Line line)
        {
            line.LName = lineNameTextBox.Text;            
            if (int.Parse(sportTypeDropDown.SelectedValue) > 0)
            {
                line.FSportType = int.Parse(sportTypeDropDown.SelectedValue);
            }
        }

        private void ClearPage()
        {
            actionTypeHidden.Value = "Add";
            lineNameTextBox.Text = string.Empty;           
        }

        private void LoadInitialValue()
        {
            Jy123.BLL.Public_Line BLLLine = new Jy123.BLL.Public_Line();
            List<Jy123.Model.Public_Line> listLine = BLLLine.GetModelList(string.Empty);
            lineRepeater.DataSource = listLine;
            lineRepeater.DataBind();            
            lineRepeater.Visible = listLine.Count > 0;

            sportTypeDropDown.Items.Clear();
            sportTypeDropDown.DataSource = listSportType;
            sportTypeDropDown.DataTextField = "SName";
            sportTypeDropDown.DataValueField = "ID";
            sportTypeDropDown.DataBind();
            sportTypeDropDown.Items.Insert(0, new ListItem("请选择", "-1"));
        }

        protected string CreateLogo(int lineId)
        {
            CreateDir createDir = new CreateDir();
            HttpPostedFile postFile = fileUpload.PostedFile;
            string logoPath = postFile.FileName;
            string logoName = string.Empty;
            if (!string.IsNullOrEmpty(logoPath))
            {
                string logoType = logoPath.Substring(logoPath.LastIndexOf(".") + 1);
                logoName = createDir.CreateUniformPath(CreateDir.UNIFORMS, lineId, logoType);
                string mapPath = Server.MapPath("/");

                Upload upload = new Upload();
                upload.PostedFile = postFile;
                upload.FileLength = postFile.ContentLength;
                upload.FileName = logoName;
                upload.Extension = logoType;
                upload.SavePath = mapPath;
                upload.UploadStart(logoName);
            }
            return logoName;
        }
    }
}
